import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class SortingTiming extends JFrame{

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SortingTiming frame = new SortingTiming();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SortingTiming() {
		setTitle("Sorting Time Comparision App");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(22, 25, 168, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(22, 131, 168, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(105, 194, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JButton btnNewButton = new JButton("Bubble Sort");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			String str = textField.getText();
			long t,time;
			t = System.nanoTime();
			int[] result1 = bubbleMethod(str);
			time = ( System.nanoTime() - t);

			textField_2.setText(""+time+ " nano secs");
			String result = "";
			for(int i=0; i<result1.length; i++){
				result=result+" "+result1[i];
			}
			textField_1.setText(result);	
				
			}
		});
		btnNewButton.setBounds(22, 75, 89, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Insertion Sort");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String str = textField.getText();
				long t,time;
				t = System.currentTimeMillis();
				int[] result1 = insertionMethod(str);
				time = (System.currentTimeMillis() - t);
				textField_2.setText(""+time+ " ms");
				String result = "";
				for(int i=0; i<result1.length; i++){
					result=result+" "+result1[i];
				}
				textField_1.setText(result);	
					
			}
		});
		btnNewButton_1.setBounds(126, 75, 115, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("Selection Sort");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = textField.getText();
				long t;
				t = System.currentTimeMillis();
				int[] result1 = selectionMethod(str);
				long  time = (System.currentTimeMillis() - t);
				
				textField_2.setText(""+time+ " ms");
				String result = "";
				for(int i=0; i<result1.length; i++){
					result=result+" "+result1[i];
				}
				textField_1.setText(result);	
					
			}
		});
		btnNewButton_2.setBounds(255, 75, 150, 23);
		contentPane.add(btnNewButton_2);
		
		JLabel lblEnterTheNumbers = new JLabel("Enter the numbers with space in between");
		lblEnterTheNumbers.setBounds(206, 28, 218, 14);
		contentPane.add(lblEnterTheNumbers);
		
		JLabel lblSortedResult = new JLabel("Sorted Result");
		lblSortedResult.setBounds(212, 134, 193, 14);
		contentPane.add(lblSortedResult);
		
		JLabel lblTimeTakenTo = new JLabel("Time taken to sort");
		lblTimeTakenTo.setBounds(214, 197, 121, 14);
		contentPane.add(lblTimeTakenTo);
	}
	
	public int[] bubbleMethod(String bubble){
		int[] resultmethod = sortBubble.SortBubble(bubble);
				return resultmethod;
	    	 
			}
	
	public int[] insertionMethod(String insert){
		int[] resultmethod = insertionSort.InsertionMethod(insert);
				return resultmethod;
	    	 
			}
	
	public int[] selectionMethod(String select){
		int[] resultmethod = selectionSort.SelectionMethod(select);
				return resultmethod;
	    	 
			}
}
